package me.zk.practice1.s;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LS17 {

    public List<String> letterCombinations(String digits) {
        List<String> res = new ArrayList<>();
        if (digits.length() == 0) {
            return res;
        }
        Map<Character,String> phoneMap = new HashMap<>();
        phoneMap.put('2', "abc");
        phoneMap.put('3', "def");
        phoneMap. put('4', "ghi");
        phoneMap. put('5', "jkl");
        phoneMap.put('6', "mno");
        phoneMap. put('7', "pqrs");
        phoneMap. put('8', "tuv");
        phoneMap. put('9', "wxyz");
        backtrack(res,phoneMap,digits,0,new StringBuffer());
        return res;

    }
    private void backtrack(List<String> res,Map<Character,String> phoneMap,String digits,
                           int index,StringBuffer sb) {
        if (index == digits.length()) {
            res.add(sb.toString());
        } else {
            char digit = digits.charAt(index);
            String letters = phoneMap.get(digit);
            int lettersCount = letters.length();
            for (int i = 0; i < lettersCount; i++) {
                sb.append(letters.charAt(i));
                backtrack(res,phoneMap,digits,index+1,sb);
                sb.deleteCharAt(index);
            }
        }


    }

    public static void main(String[] args) {
        System.out.println(new LS17().letterCombinations("234"));
    }
}
